package org.jeecg.modules.yw.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.aliyuncs.utils.StringUtils;

/**
 * 时间工具类
 * @author cs
 *
 */
public class DateUtils {
	
	public static String dateTimePattern = "yyyy-MM-dd HH:mm:ss";
	
	public static String datePattern = "yyyy-MM-dd";

	public static Date getCurrentDate() {
		return Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
	}
	public static Date strToDateTime(String date) {
		if(StringUtils.isEmpty(date)||date.equals("1970-01-01")){
			return null;
		}
		  // 创建SimpleDateFormat对象，指定字符串的格式
        SimpleDateFormat formatter = new SimpleDateFormat(dateTimePattern);
        Date date1 = null;
        try {
            // 使用parse方法将字符串转换为Date对象
            date1 = formatter.parse(date);
            // 输出转换后的日期
            System.out.println("转换后的时间是: " + date1);
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
        }
        return date1;
	}
	
	public static Date strToDate(String date) {
		  // 创建SimpleDateFormat对象，指定字符串的格式
      SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
      Date date1 = null;
      try {
          // 使用parse方法将字符串转换为Date对象
          date1 = formatter.parse(date);
          // 输出转换后的日期
          System.out.println("转换后的日期是: " + date1);
      } catch (Exception e) {
          // 处理可能发生的异常
          e.printStackTrace();
      }
      return date1;
	}
	
	public static String dateTimeToStr(Date dateTime) {
		SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
		String dateStr = sf.format(dateTime);
		return dateStr;
	}
	
	public static String dateToStr(Date date) {
		SimpleDateFormat sf = new SimpleDateFormat(datePattern);
		String dateStr = sf.format(date);
		return dateStr;
	}
		
	/**
	 * 得到当前时间
	 * @return
	 */
	public static String getCurrentTimeDateStr() {
		SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
		String dateTimeStr = sf.format(new Date());
		return dateTimeStr;
	}
	/**
	 * 得到当前日期
	 * @return
	 */
	public static String getDateStr() {
		SimpleDateFormat sf = new SimpleDateFormat(datePattern);
		String dateTimeStr = sf.format(new Date());
		return dateTimeStr;
	}
	
	
	/**
	 * 得到昨天日期
	 * @return
	 */
	public static String getYesterDateStr() {
		LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
        String formattedYesterday = yesterday.format(formatter);
        return formattedYesterday;
	}


	// 获取当天开始时间的字符串
	public static String getTodayStartTimeStr() {
		Calendar calendar = Calendar.getInstance();
		// 设置小时、分钟、秒和毫秒为 0
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
		String dateTimeStr = sf.format(calendar.getTime());
		return dateTimeStr;
	}

	// 获取当天结束时间的字符串
	public static String getTodayEndTimeStr() {
		Calendar calendar = Calendar.getInstance();
		// 设置小时为 23，分钟和秒为 59，毫秒为 999
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
		String dateTimeStr = sf.format(calendar.getTime());
		return dateTimeStr;
	}

	// 获取指定日期当天开始时间的字符串
	public static String getDayStartTimeStr(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
		return sf.format(calendar.getTime());
	}

	// 获取指定日期当天结束时间的字符串
	public static String getDayEndTimeStr(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
		return sf.format(calendar.getTime());
	}
	public static boolean isDateTime(String string){
		boolean flag = false;
		try {
			SimpleDateFormat sf = new SimpleDateFormat(dateTimePattern);
			sf.parse(string);
			flag = true;
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	/**
     *  返回加入的天数的日期
     * @param startDt
     * @param addDays
     */
    public  static String flagMonthFirstDay(String startDt, String addDays){
        String start = "";
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = dateFormat.parse(startDt);
            Calendar cal = Calendar.getInstance();
            cal.setTime(startTime);  // 将Date类型转换为Calendar类型
            cal.add(Calendar.DATE, Integer.valueOf(addDays));  // 加addDay
            Date rt = cal.getTime();// 将Calendar类型转换为Date类型
            start = dateFormat.format(rt);
            return start;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return start;
    }
    
    /**
     * 计算2个时间相差的天数、小时、分钟、秒
     * @param startTime 开始时间
     * @param endTime 截止时间
     * @param format 时间格式 yyyy-MM-dd HH:mm:ss
     * @param str 返回的数据为：day-天、hour-小时、min-分钟、second-秒
     * @return
     */
    public static Long dateDiff(String startTime, String endTime,  String format, String str) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long second = 0;
        // 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            // 计算差多少天
            day = diff / nd;
            // 计算差多少小时
            hour = diff / nh ;
            // 计算差多少分钟
            min = diff / nm ;
            // 计算差多少秒
            second = diff / ns;
            // 输出结果
            System.out.println("时间相差：" + day + "天" +
                      (hour - day * 24) + "小时"
                    + (min - day * 24 * 60) + "分钟" +
                    second + "秒。");
            /*System.out.println("hour=" + hour + ",min=" + min);*/
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (str.equalsIgnoreCase("day")) {
            return day;
        } else if(str.equalsIgnoreCase("hour")) {
            return hour;
        } else if(str.equalsIgnoreCase("min")) {
            return min;
        } else {
            return second;
        }
    }
    
    public static List<String> getAllDate(String startDate, String endDate) {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	List<String> list = new ArrayList<>();
    	try {
			//起始日期
			Date d1 = sdf.parse(startDate);
			//结束日期
			Date d2 = sdf.parse(endDate);
			Date tmp=d1;
			Calendar dd =Calendar.getInstance();
			dd.setTime(d1);
			//打印2001年10月1日到2001年11月4日的日期
			while(tmp.getTime()<d2.getTime()) {
				tmp=dd.getTime();
				list.add(sdf.format(tmp));
				System.out.println(sdf.format(tmp));
				//天数加上1
				dd.add(Calendar.DAY_OF_MONTH, 1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return list;
    }
	public static String convertToDate(String dateTimeStr) {
		// 定义原始日期时间格式
		DateTimeFormatter originalFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		// 定义目标日期格式
		DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		// 解析原始日期时间字符串
		LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, originalFormatter);
		// 格式化日期为目标格式
		return dateTime.format(targetFormatter);
	}
	
	public static void main(String args[]) {
		System.out.println(getYesterDateStr());
		System.out.println(strToDate(getCurrentTimeDateStr()));
		String s = convertToDate("2025-04-25 00:00:00");
		System.out.println(s+"````````````````````````````````````````````````" +
				"````````````````````````````````````````");
		List<String> list = getAllDate("2025-04-25","2025-04-29");
	}
}
